package com.lsq.tucao.express.service.serviceImpl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsq.tucao.express.bean.po.TucaoDiscuss;
import com.lsq.tucao.express.dao.TucaoDetailMapper;
import com.lsq.tucao.express.dao.TucaoDiscussMapper;
import com.lsq.tucao.express.response.JsonResult;
import com.lsq.tucao.express.service.TucaoDiscussService;
import com.lsq.tucao.express.utils.SensitiveWordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class TucaoDiscussServiceImpl implements TucaoDiscussService {

    @Autowired
    private TucaoDiscussMapper tucaoDiscussMapper;
    @Autowired
    private TucaoDetailMapper tucaoDetailMapper;
    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    @Override
    public JsonResult getPageDiscussByTucaoId(Integer current, Integer size, Long tucaoId) {
        if(tucaoId != null && tucaoId != 0){
            Page<TucaoDiscuss> page = new Page<>(current,size);
            QueryWrapper<TucaoDiscuss> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(TucaoDiscuss::getTucaoId,tucaoId)
                    .orderByDesc(TucaoDiscuss::getCreateTime);

            Page<TucaoDiscuss> tucaoDiscussPage = tucaoDiscussMapper.selectPage(page, queryWrapper);
            if(tucaoDiscussPage != null){
                return new JsonResult(tucaoDiscussPage,200,"分页查询吐槽评论成功");
            }else {
                return new JsonResult(500,"分页查询吐槽评论失败");
            }
        }else {
            return new JsonResult(500,"请传入有效的吐槽Id");
        }
    }

    @Override
    public JsonResult addDiscuss(TucaoDiscuss tucaoDiscuss) {
        if(tucaoDiscuss != null && tucaoDiscuss.getTucaoId() != null && !StringUtils.isEmpty(tucaoDiscuss.getContent())){
            //检测发布的吐槽中是否包含敏感词汇
            Set<String> set = sensitiveWordFilter.getSensitiveWord(tucaoDiscuss.getContent(), 1);
            if(set.size() > 0){
                return new JsonResult(500,"发布的吐槽中含有违禁词"+set);
            }else {
                int update = tucaoDetailMapper.discussTucao(tucaoDiscuss.getTucaoId());
                int insert = tucaoDiscussMapper.insert(tucaoDiscuss);
                if(insert > 0 && update > 0){
                    return new JsonResult(200,"发布吐槽评论成功");
                }else {
                    return new JsonResult(500,"发布吐槽评论失败");
                }
            }
        }else {
            return new JsonResult(500,"请输入要发表的评论内容");
        }
    }
}
